class Solution:
    def splitArray(self, nums: list, m: int) -> int:
        left = max(nums)
        right = sum(nums)
        while left < right:
            mid = (left + right) // 2
            cnt = 1
            cur_sum = 0
            for num in nums:
                if cur_sum + num > mid:
                    cur_sum = 0
                    cnt += 1
                cur_sum += num
            if cnt <= m:
                right = mid
            else:
                left = mid + 1

        return right
